<%@page import="java.io.BufferedOutputStream"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="com.jspsmart.upload.SmartUpload"%>
<%@page import="java.util.UUID"%>
<%@page import="com.softeem.sss.dto.DownRecord"%>
<%@page import="com.softeem.sss.dao.DownRecordDAO"%>
<%@page import="com.softeem.sss.dao.UserDAO"%>
<%@page import="java.io.File"%>
<%@page import="com.softeem.sss.dto.User"%>
<%@page import="com.softeem.sss.dto.FileInfo"%>
<%@page import="com.softeem.sss.dao.FileInfoDAO"%>
<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ include file="doCheckSession.jsp" %>
<%

	
	//获取需要被下载的文件id	
	String id = request.getParameter("fid");
	//将文件id转换为整数类型
	int fid = Integer.parseInt(id);
	
	FileInfoDAO fdao = new FileInfoDAO();
	//获取需要被下载文件信息
	FileInfo fi = fdao.findByFid(fid);
	//将session中的对象强制转换为用户对象
	User user = (User)obj;
	
	//判断用户积分是否足够
	if(user.getIntergral() < fi.getSaleIntergral()){
		request.setAttribute("msg", "积分不足,需要"+fi.getSaleIntergral()+",缺少:"+(fi.getSaleIntergral()-user.getIntergral()));
		request.getRequestDispatcher("doList.jsp").forward(request, response);
		return;
	}
	
	//准备开始下载
	//sources目录在服务器中的绝对路径(文件资源存储的根目录)
	String basePath = request.getServletContext().getRealPath("sources");
	//文件资源的相对路径
	String path = fi.getPath();
	//获取文件所在绝对路径
	String realpath = basePath + File.separator + path;
	//文件不存在时
	if(!new File(realpath).exists()){
		//逻辑删除该行记录
		fdao.delById(fid);
		request.setAttribute("msg", "下载错误，文件不存在");
		request.getRequestDispatcher("doList.jsp").forward(request, response);
		return;
	}
	
	//修改数据库表（减少用户积分，向下载记录表中插入数据）
	UserDAO udao = new UserDAO();
	//减少下载者的积分
	user.setIntergral(user.getIntergral() - fi.getSaleIntergral());//修改积分
	udao.updateIntegral(user);
	
	//增加上传者积分
	User u2 = udao.findByUid(fi.getUid());//查询上传者信息
	u2.setIntergral(u2.getIntergral() + fi.getSaleIntergral());
	udao.updateIntegral(u2);
	
	//修改文件的下载次数
	fdao.updateDcountByFid(fi.getFid());
	
	//向下载记录表中插入数据
	DownRecordDAO drdao = new DownRecordDAO();
	DownRecord dr = new DownRecord();
	dr.setUid(user.getUid());
	dr.setFid(fi.getFid());
	dr.setRid(UUID.randomUUID().toString());
	drdao.insert(dr);//插入
	
	//下载文件(smartupload组件实现)
	SmartUpload su = new SmartUpload();
	su.initialize(this,request,response);
	su.setContentDisposition(null);
	su.downloadFile(realpath);
	
	out.clear();
	out = pageContext.pushBody();
	
	//原生IO实现文件下载
// 	response.setHeader("Content-Desposition", "attachment;filename="+fi.getFname());
// 	response.setContentType("*/*");
// 	RequestDispatcher rd = request.getRequestDispatcher("/sources/"+path);
// 	rd.forward(request, response);
	
// 	BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
// 	FileInputStream fis = new FileInputStream(realpath);
// 	BufferedInputStream bis = new BufferedInputStream(fis);
// 	byte[] b = new byte[1024];
// 	int len = 0;
// 	while((len = bis.read(b)) != -1){
// 		bos.write(b, 0, len);
// 	}
// 	bos.close();
// 	bis.close();

%>
